package com.duobaoyu.dby.supply.job.task.channel;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.ChannelAccessTokenBoundaryClient;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.dto.BatchRefreshTokenDTO;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ChannelAuthTokenTask
 * @Description 渠道token刷新
 * @createTime 15:30:00 2023/07/08
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ChannelAuthTokenTask {

    private final ChannelAccessTokenBoundaryClient channelAccessTokenBoundaryClient;

    /**
     * 渠道定时Token刷新
     */
    @XxlJob("channelRefreshTokenJobHandler")
    public void channelRefreshToken() {
        String tokenJson = XxlJobHelper.getJobParam();
        BatchRefreshTokenDTO param = null;
        if(StringUtils.isNotBlank(tokenJson) && JSONUtil.isTypeJSON(tokenJson)) {
            param = JSON.parseObject(tokenJson, BatchRefreshTokenDTO.class);
        }
        channelAccessTokenBoundaryClient.batchRefreshChannelToken(param);
    }
}
